package charlie.shop.admin;

import com.charlie.beans.Users;
import com.charlie.db.DAOLocal;
import com.charlie.sessions.UsersFacadeLocal;
import java.io.IOException;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Delete user servlet
 *
 * @author Оленка
 */
@WebServlet(name = "DeleteUser", urlPatterns = {"/admin/deleteUser"})
public class DeleteUser extends HttpServlet {

    @EJB
    UsersFacadeLocal usersFacade;
    @EJB
    DAOLocal dao;
    
    Users user;   

    /**
     * Delete selected user
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (user != null) {
            user.setUserStatus(2);
            usersFacade.edit(user);
        }
        getServletContext().getRequestDispatcher("/admin/completeDeletion.jsp").forward(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String id = request.getParameter("userId");
        user = usersFacade.findByUsersId(Integer.valueOf(id));
        //for checking admin role
        List<String> roles = dao.getUserPersonalRolesByEmail(user.getEmail());
        request.setAttribute("userroles", roles);       
        getServletContext().getRequestDispatcher("/admin/deletion.jsp").forward(request, response);
    }
}
